package com.example.portal.tabel.service;

import com.example.portal.colmapping.dao.ColMappingDao;
import com.example.portal.colmapping.model.ColMappingDO;
import com.example.portal.tabel.dao.TableDao;
import com.example.portal.tabel.model.TableDO;
import com.example.util.GUID;
import com.example.util.PageResult;
import com.example.util.RowBoundUtil;
import com.example.util.StringUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class TableServiceImpl {

    @Resource
    TableDao tableDao;

    @Resource
    RowBoundUtil rowBoundUtil;

    /**
     * 分页查询
     * @param params
     * @return
     */

    public PageResult<List<TableDO>> getTables(Map<String, Object> params){
        String pageIndex = params.get("pageIndex") == null ? "" : params.get("pageIndex").toString();
        String pageSize = params.get("pageSize") == null ? "" : params.get("pageSize").toString();
        String key = params.get("key") == null ? "" : params.get("key").toString();
        List<TableDO> tableDOS = new ArrayList<>();
        int count = 0;
        Example example = new Example(ColMappingDO.class);
        if(!StringUtil.isNullOrEmpty(key)){
            Example.Criteria criteria = example.createCriteria();
            criteria.andCondition(" table_name like '%"+key+"%' OR  name like '%"+key +"'");
            count = tableDao.selectCountByExample(example);
        }else{
            count = tableDao.selectAll().size();
        }
        RowBounds rowBounds = rowBoundUtil.getRowBounds(pageSize, pageIndex);
        example.setOrderByClause(" table_name desc");
        tableDOS = tableDao.selectByExampleAndRowBounds(example,rowBounds);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(count);
        pageResult.setData(tableDOS);
        return pageResult;
    }


    /**
     * 插入定义
     */

    public  void insertTables(Map<String, Object> params) {
        String id = GUID.newGUID();
        TableDO tableDO = new TableDO();
        tableDO.setId(id);
        tableDao.insert(tableDO);
    }

    /**
     * 删除定义
     * @param params
     */
    public  void delTables(Map<String, Object> params){
        List<String> ids = (List<String>) params.get("ids");
        if (ids.size() > 0) {
            Example example = new Example(TableDO.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andIn("id", ids);
            tableDao.deleteByExample(example);
        }
    }

    /**
     * 更新定义
     * @param params
     */
    public void updateTables(Map<String, Object> params){
        List<Map<String, Object>> dataList = (List<Map<String, Object>>) params.get("data");
        if (dataList.size() > 0) {
            for (Map<String, Object> data : dataList) {

                TableDO tableDO = new TableDO();
                tableDO.setId(data.get("id").toString());
                if(data.get("excelPath")!=null) {
                    if(!StringUtil.isNullOrEmpty(data.get("excelPath").toString())){
                        tableDO.setExcelPath((data.get("excelPath").toString()));
                    }
                }
                if(data.get("tableName")!=null) {
                    if(!StringUtil.isNullOrEmpty(data.get("tableName").toString())){
                        tableDO.setTableName((data.get("tableName").toString()));
                    }
                }
                if(data.get("name")!=null) {
                    if(!StringUtil.isNullOrEmpty(data.get("name").toString())){
                        tableDO.setName((data.get("name").toString()));
                    }
                }
                if(data.get("bakPath")!=null) {
                    if(!StringUtil.isNullOrEmpty(data.get("bakPath").toString())){
                        tableDO.setBakPath((data.get("bakPath").toString()));
                    }
                }
                tableDao.updateByPrimaryKey(tableDO);
            }
        }
    }

}
